﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Repositories.Interfaces;
using Repositories;

namespace AsiaSnackExpress
{
    public partial class Customers : System.Web.UI.Page
    {
        #region Properties


        ICustomersRepository _CustomersRep;
        public ICustomersRepository CustomersRep
        {
            get
            {
                if (_CustomersRep == null)
                {
                    _CustomersRep = DIHelper.Resolve<ICustomersRepository>();
                }
                return _CustomersRep;
            }

        }

        public List<Repositories.Customers> CustomersDataSource
        {
            get
            {
                return (List<Repositories.Customers>)gridCustomers.DataSource;
            }
            set
            {
                if (value != null && value.Count > 0)
                {
                    lblErrorMessage.Visible = false;
                    gridCustomers.Visible = true;
                    gridCustomers.DataSource = value;
                    gridCustomers.DataBind();
                }
                else
                {
                    lblErrorMessage.Visible = true;
                    gridCustomers.Visible = false;
                }
            }
        }


        #endregion

        protected override void OnLoad(EventArgs e)
        {
            DIHelper.Configure();
            base.OnLoad(e);
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
                CustomersDataSource = CustomersRep.GetAllCustomers(txtSearch.Text);

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            CustomersDataSource = CustomersRep.GetAllCustomers(txtSearch.Text);
        }

        protected void CustomersGridView_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (gridCustomers.SelectedIndex >= 0)
            {
                Repositories.Customers cus;
                if (gridCustomers.SelectedDataKey != null && (int)gridCustomers.SelectedDataKey.Value > 0)
                {
                    cus = CustomersRep.FindById((int)gridCustomers.SelectedDataKey.Value);
                    if (cus != null)
                    {
                        lblUpdateMessage.Visible = false;
                        panelUpdate.Visible = true;
                        btnUpdate.Visible = true;
                        btnDelete.Visible = true;
                        btnAdd.Visible = false;
                        MapToObject(cus);

                    }
                }
            }            
        }

        private void MapToObject(Repositories.Customers cus)
        {
            txtCustomerId.Text = cus.CustomerId.ToString();
            txtFirstName.Text = cus.FirstName;
            txtLastName.Text = cus.LastName;
            txtRoad.Text = cus.Road;
            txtZipCode.Text = cus.ZipCode;
            txtCity.Text = cus.City;
            txtCountry.Text = cus.Country;
            txtMobi.Text = cus.Mobi;
            txtTelephone.Text = cus.Telephone;
            txtHomeTel.Text = cus.HomeTel;
            txtEmail.Text = cus.Email;
            txtBirthday.Text = cus.Birthday != null ? cus.Birthday.Value.ToShortDateString() : "";
            txtDescription.Text = cus.Description;
        }

        private void ClearForm()
        {
            txtCustomerId.Text = "0";
            txtFirstName.Text = "";
            txtLastName.Text = "";
            txtRoad.Text = "";
            txtZipCode.Text = "";
            txtCity.Text = "";
            txtCountry.Text = "";
            txtMobi.Text = "";
            txtTelephone.Text = "";
            txtHomeTel.Text = "";
            txtEmail.Text = "";
            txtBirthday.Text = DateTime.Today.ToShortDateString();
            txtDescription.Text = "";
        }

        protected void btnNewCustomer_Click(object sender, EventArgs e)
        {
            ClearForm();
            panelUpdate.Visible = true;
            btnDelete.Visible = false;
            txtFirstName.Focus();
            btnUpdate.Visible = false;
            btnDelete.Visible = false;
            btnAdd.Visible = true;
            gridCustomers.SelectedIndex = -1;
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Repositories.Customers cus = new Repositories.Customers();
            MapFromUI(cus);
            CustomersRep.Insert(cus);
            CustomersRep.ApplyTheChanges();
            lblUpdateMessage.Text = "Add successfully.";
            lblUpdateMessage.Visible = true;
            txtSearch.Text = "";
            btnSearch_Click(sender, e);
            panelUpdate.Visible = false;
            
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            Repositories.Customers cus = CustomersRep.FindById((int)gridCustomers.SelectedDataKey.Value);
            if (cus != null)
            {
                MapFromUI(cus);
                CustomersRep.Update(cus);
                CustomersRep.ApplyTheChanges();
                lblUpdateMessage.Text = "Update successfully.";
                lblUpdateMessage.Visible = true;
                btnDelete.Visible = true;
            }

            btnSearch_Click(sender, e);
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            Repositories.Customers cus = CustomersRep.FindById((int)gridCustomers.SelectedDataKey.Value);
            if (cus != null)
            {                
                CustomersRep.DeleteCustomers(cus);
                CustomersRep.ApplyTheChanges();
                lblUpdateMessage.Text = "Delete successfully.";
                lblUpdateMessage.Visible = true;
                panelUpdate.Visible = false;
            }

            btnSearch_Click(sender, e);
        }



        private void MapFromUI(Repositories.Customers cus)
        {
            cus.FirstName = txtFirstName.Text.Trim();
            cus.LastName = txtLastName.Text.Trim();
            cus.Road = txtRoad.Text.Trim();
            cus.ZipCode = txtZipCode.Text.Trim();
            cus.City = txtCity.Text.Trim();
            cus.Country = txtCountry.Text.Trim();
            cus.Mobi = txtMobi.Text.Trim();
            cus.Telephone = txtTelephone.Text.Trim();
            cus.HomeTel = txtHomeTel.Text.Trim();
            cus.Email = txtEmail.Text.Trim();
            if (!string.IsNullOrEmpty(txtBirthday.Text.Trim()))
                try
                {
                    cus.Birthday = DateTime.Parse(txtBirthday.Text.Trim());
                }
                catch
                { }
            cus.Description = txtDescription.Text.Trim();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            lblUpdateMessage.Visible = false;
            btnSearch_Click(sender, e);
            gridCustomers.SelectedIndex = -1;
            panelUpdate.Visible = false;
        }

    }
}